package com.yxx.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yxx.business.entity.*;
import com.yxx.business.service.PhaseService;
import com.yxx.common.utils.DateUtils;
import com.yxx.common.core.domain.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.core.service.impl.ServiceImplPlus;
import com.yxx.business.mapper.TbSurveyDesignPhaseMapper;
import com.yxx.business.service.ITbSurveyDesignPhaseService;

/**
 * 勘察设计阶段Service业务层处理
 *
 * @author yxx
 * @date 2025-08-16
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbSurveyDesignPhaseServiceImpl extends ServiceImplPlus<TbSurveyDesignPhaseMapper, TbSurveyDesignPhaseDo> implements ITbSurveyDesignPhaseService, PhaseService {

    private final TbSurveyDesignPhaseMapper tbSurveyDesignPhaseMapper;

    /**
     * 查询勘察设计阶段分页结果
     *
     * @param tbSurveyDesignPhaseQueryDto 勘察设计阶段查询实体
     * @return 勘察设计阶段分页
     */
    @Override
    public PageResult<TbSurveyDesignPhaseVo> selectTbSurveyDesignPhaseVoPage(TbSurveyDesignPhaseQueryDto tbSurveyDesignPhaseQueryDto) {
        startPage();
        PageResult<TbSurveyDesignPhaseVo> page
                = super.getMyBatisPageResult(selectTbSurveyDesignPhaseDoList(tbSurveyDesignPhaseQueryDto), TbSurveyDesignPhaseVo.class);
        clearPage();
        return page;
    }

    /**
     * 查询勘察设计阶段Vo列表
     *
     * @param tbSurveyDesignPhaseQueryDto 勘察设计阶段查询实体
     * @return 勘察设计阶段集合
     */
    @Override
    public List<TbSurveyDesignPhaseVo> selectTbSurveyDesignPhaseVoList(TbSurveyDesignPhaseQueryDto tbSurveyDesignPhaseQueryDto) {
        return super.convertVoList(tbSurveyDesignPhaseMapper.selectTbSurveyDesignPhaseList(tbSurveyDesignPhaseQueryDto), TbSurveyDesignPhaseVo.class);
    }

    /**
     * 查询勘察设计阶段Do列表
     *
     * @param tbSurveyDesignPhaseQueryDto 勘察设计阶段查询实体
     * @return 勘察设计阶段集合
     */
    @Override
    public List<TbSurveyDesignPhaseDo> selectTbSurveyDesignPhaseDoList(TbSurveyDesignPhaseQueryDto tbSurveyDesignPhaseQueryDto) {
        return tbSurveyDesignPhaseMapper.selectTbSurveyDesignPhaseList(tbSurveyDesignPhaseQueryDto);
    }

    /**
     * 查询单个勘察设计阶段
     *
     * @param designId 勘察设计阶段主键
     * @return 勘察设计阶段单个
     */
    @Override
    public TbSurveyDesignPhaseVo selectTbSurveyDesignPhaseVoByDesignId(Long designId) {
        return super.convertBean(tbSurveyDesignPhaseMapper.selectTbSurveyDesignPhaseByDesignId(designId), TbSurveyDesignPhaseVo.class);
    }

    /**
     * 新增勘察设计阶段
     *
     * @param tbSurveyDesignPhaseQueryDto 勘察设计阶段编辑实体
     * @return 结果
     */
    @Override
    public int insertTbSurveyDesignPhase(TbSurveyDesignPhaseEditDto tbSurveyDesignPhaseQueryDto) {
        tbSurveyDesignPhaseQueryDto.setCreateById(getUserIdOrNotLogged());
        tbSurveyDesignPhaseQueryDto.setCreateBy(getUserNameOrNotLogged());
        tbSurveyDesignPhaseQueryDto.setCreateTime(DateUtils.getNowDate());
        return tbSurveyDesignPhaseMapper.insertTbSurveyDesignPhase(super.convertT(tbSurveyDesignPhaseQueryDto));
    }

    /**
     * 修改勘察设计阶段
     *
     * @param designId 勘察设计阶段主键
     * @param tbSurveyDesignPhaseQueryDto 勘察设计阶段编辑实体
     * @return 结果
     */
    @Override
    public int updateTbSurveyDesignPhase(Long designId, TbSurveyDesignPhaseEditDto tbSurveyDesignPhaseQueryDto) {
        tbSurveyDesignPhaseQueryDto.setUpdateBy(getUserNameOrNotLogged());
        tbSurveyDesignPhaseQueryDto.setUpdateTime(DateUtils.getNowDate());
        return tbSurveyDesignPhaseMapper.updateTbSurveyDesignPhase(super.convertT(tbSurveyDesignPhaseQueryDto).setDesignId(designId));
    }

    /**
     * 批量删除勘察设计阶段
     *
     * @param designIds 需要删除的勘察设计阶段主键集合
     * @return 结果
     */
    @Override
    public int deleteTbSurveyDesignPhaseByDesignIds(List<Long> designIds) {
        return tbSurveyDesignPhaseMapper.deleteTbSurveyDesignPhaseByDesignIds(designIds);
    }

    /**
     * 删除单个勘察设计阶段信息
     *
     * @param designId 勘察设计阶段主键
     * @return 结果
     */
    @Override
    public int deleteTbSurveyDesignPhaseByDesignId(Long designId) {
        return tbSurveyDesignPhaseMapper.deleteTbSurveyDesignPhaseByDesignId(designId);
    }

    @Override
    public TbEquipmentPhaseVo getPhase(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        return convertBean(tbSurveyDesignPhaseMapper.selectOne(
                        new LambdaQueryWrapper<TbSurveyDesignPhaseDo>().eq(TbSurveyDesignPhaseDo::getEngineeringId, tbEquipmentPhaseDto.getEngineeringId()).eq(TbSurveyDesignPhaseDo::getProjectId, tbEquipmentPhaseDto.getProjectId())),
                TbEquipmentPhaseVo.class);
    }

    @Override
    public int savePhase(TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        Long l = tbSurveyDesignPhaseMapper.selectCount(new LambdaQueryWrapper<TbSurveyDesignPhaseDo>().eq(TbSurveyDesignPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()));
        if (l > 0) {
            tbSurveyDesignPhaseMapper.update(convertT(tbEquipmentPhaseVo), new LambdaUpdateWrapper<TbSurveyDesignPhaseDo>().eq(TbSurveyDesignPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()).eq(TbSurveyDesignPhaseDo::getProjectId, tbEquipmentPhaseVo.getProjectId()));
        } else {
            tbSurveyDesignPhaseMapper.insert(convertT(tbEquipmentPhaseVo));
        }
        return 1;
    }

    @Override
    public int updatePhase(Long id, TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        return tbSurveyDesignPhaseMapper.updateById(convertT(tbEquipmentPhaseVo).setDesignId(id));
    }
}
